#include "dev/dacai/driver/driver.h"
#include "dev/dacai/driver/queue.h"
#include "usart.h"

static struct t_driver *_driver = {0};

static void ProcessMessage(struct t_driver *driver,uint16 size)
{
    PCTRL_MSG msg = (PCTRL_MSG)driver->cmd_buffer;

    uint8 cmd_type = msg->cmd_type;                                                  //指令类型
    uint8 ctrl_msg = msg->ctrl_msg;                                                  //消息的类型
    uint8 control_type = msg->control_type;                                          //控件类型
    uint16 screen_id = PTR2U16(&msg->screen_id);                                     //画面ID
    uint16 control_id = PTR2U16(&msg->control_id);                                   //控件ID
    uint32 value = PTR2U32(msg->param);
    //数值
    uint8 *cmdbuffer = (uint8 *)msg;

    switch(cmd_type)
    {
        case NOTIFY_TOUCH_PRESS:                                                        //触摸屏按下
        case NOTIFY_TOUCH_RELEASE:                                                      //触摸屏松开
             break;
        case NOTIFY_WRITE_FLASH_OK:                                                     //写FLASH成功
             break;
        case NOTIFY_WRITE_FLASH_FAILD:                                                  //写FLASH失败
             break;
        case NOTIFY_READ_FLASH_OK:                                                      //读取FLASH成功
             break;
        case NOTIFY_READ_FLASH_FAILD:                                                   //读取FLASH失败
             break;
        case NOTIFY_READ_RTC:                                                           //读取RTC时间
             driver->_NotifyReadRTC(*(cmdbuffer+2),*(cmdbuffer+3),*(cmdbuffer+4),*(cmdbuffer+5),*(cmdbuffer+6),*(cmdbuffer+7),*(cmdbuffer+8));
             break;
        case NOTIFY_CONTROL:
        {
            if(ctrl_msg==MSG_GET_CURRENT_SCREEN)                                    //画面ID变化通知
            {
                driver->_NotifyScreen(screen_id);                                            //画面切换调动的函数
            }
            else
            {
                switch(control_type)
                {
                    case kCtrlButton:                                                   //按钮控件
                         driver->_NotifyButton(screen_id,control_id,msg->param[1]);
                         break;
                    case kCtrlText:                                                     //文本控件
                         driver->_NotifyText(screen_id,control_id,msg->param);
                         break;
                    case kCtrlProgress:                                                 //进度条控件
                         driver->_NotifyProgress(screen_id,control_id,value);
                         break;
                    case kCtrlSlider:                                                   //滑动条控件
                         driver->_NotifySlider(screen_id,control_id,value);
                         break;
                    case kCtrlMeter:                                                    //仪表控件
                         driver->_NotifyMeter(screen_id,control_id,value);
                         break;
                    case kCtrlMenu:                                                     //菜单控件
                         driver->_NotifyMenu(screen_id,control_id,msg->param[0],msg->param[1]);
                         break;
                    case kCtrlSelector:                                                 //选择控件
                         driver->_NotifySelector(screen_id,control_id,msg->param[0]);
                         break;
                    case kCtrlRTC:                                                      //倒计时控件
                         driver->_NotifyTimer(screen_id,control_id);
                         break;
                    default:
                         break;
                }
            }
            break;
        }
        case NOTIFY_HandShake:                                                          //握手通知
             driver->_NOTIFYHandShak();
             break;
        default:
             break;
    }
}

void driver_init(struct t_driver *driver)
{
   _driver = driver;
   usart4_init(115200);
   queue_reset();
	
}
void driver_poll(void)
{
  uint8 data=0;
  uint8 ret = usart4_receive(&data);
  if(ret)
  {
    queue_push(data);
  }

  uint32 size = 0;

  size = queue_find_cmd(_driver->cmd_buffer,CMD_MAX_SIZE);

  if(size>0&&*(_driver->cmd_buffer+1)!=0x07)
  {
     ProcessMessage(_driver, size);
  }
}
